package com.csr.csrcheck.controller;

import com.csr.csrcheck.controller.ex.CompanyException;
import com.csr.csrcheck.pojo.Actual_controller;
import com.csr.csrcheck.pojo.Company;
import com.csr.csrcheck.pojo.Special_bulletin;
import com.csr.csrcheck.service.Actual_controllerService;
import com.csr.csrcheck.service.CompanyService;
import com.csr.csrcheck.util.JsonResult;
import com.csr.csrcheck.util.PageResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RequestMapping({"company"})
@RestController
public class CompanyConntroller extends BaseController {
    private static final Logger log = LoggerFactory.getLogger(com.csr.csrcheck.controller.CompanyConntroller.class);

    @Resource
    private CompanyService companyService;

    @Resource
    private Actual_controllerService actual_controllerService;

    @GetMapping({"companylist"})
    public JsonResult<List<Company>> Companylist() {
        List<Company> list = this.companyService.getCommpanylist();
        if (list == null)
            throw new CompanyException("");
        return new JsonResult(Integer.valueOf(0), "json", list);
    }

    @GetMapping({"companylist2"})
    public JsonResult<List<Company>> CompanyList2(String name) {
        List<Company> list2 = this.companyService.getCommpanylistbyname(name);
        if (list2 == null)
            throw new CompanyException("");
        if (name.equals("") || name == null)
            throw new CompanyException("");
        return new JsonResult(Integer.valueOf(0), "json", list2);
    }

    @RequestMapping({"companypage"})
    public Object findCompanyPage(@RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "5") int pageSize, String company_name) {
        PageResult page = this.companyService.findPage(pageNum, pageSize, company_name);
        if (page == null)
            throw new CompanyException("");
        return page;
    }

    @GetMapping({"special"})
    public JsonResult<List<Special_bulletin>> getAll() {
        List<Special_bulletin> list = this.companyService.getAll();
        return new JsonResult(Integer.valueOf(0), "json", list);
    }

    @RequestMapping(path = {"addcompany"}, method = {RequestMethod.POST})
    public JsonResult<Object> addcompany(Company company) {
        this.companyService.addCompany(company);
        log.info("add================================>approvals"+ company);
        return new JsonResult(Integer.valueOf(0), "json");
    }

    @RequestMapping(path = {"listactual"}, method = {RequestMethod.POST})
    public JsonResult<List<Actual_controller>> listactual() {
        List list = this.actual_controllerService.list();
        return new JsonResult(Integer.valueOf(0), "json", list);
    }

    @RequestMapping(path = {"updatecompany"}, method = {RequestMethod.POST})
    public JsonResult<Void> updatecompany(Company company) {
        this.companyService.updateCompany(company);
        return new JsonResult(Integer.valueOf(0), "json");
    }

    @RequestMapping(path = {"findCompanyByid/{id}"}, method = {RequestMethod.POST})
    public JsonResult<Company> findCompanyByid(@PathVariable("id") int id) {
        Company list = null;
        try {
            list = this.companyService.findcompanyByid(id);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new JsonResult(Integer.valueOf(0), "json", list);
    }

    @RequestMapping(path = {"deletecompany/{id}"}, method = {RequestMethod.POST})
    public JsonResult<Void> deletecompany(@PathVariable("id") int id) {
        this.companyService.deletecompanyByid(id);
        log.info(""+id);
        return new JsonResult(Integer.valueOf(0), "json");
    }
}
